package com.example.teststatisticesinterface.activity;

import com.example.teststatisticesinterface.R;
import com.oppo.statistics.open.MobileClickAgent;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.content.Intent;
import android.text.Layout;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;
import android.widget.ToggleButton;

public class MainActivity extends Activity {
	private Button mPageVisit, mAppStart, mUserAction, mUserActLocation, mShare, mSpecialClick, mClientDown, mOnEvent_simple, mOnEvent_count, mOnEvent_Mult_Tag, mOnEvent_KV, mUpAll,mdb,mdb_table,btn_debuggable;
	private ToggleButton mToggleButton;
	
	private static int appCode = 111111;
	private static int actionCode = 22222;
	private static int actPosition = 33333;
	private static String actOther = "test";

	private Intent mIntent;

	private static int pid = 11;
	private static int cid = 22;
	private static int shareto = 33;
	
	private boolean isExit=false;
	
	private int count=0;
	private boolean isCounted=true;
	private boolean getIsCounted(){
		return isCounted;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
//		showDisplayMetrics();
		// activity onCreate()
		MobileClickAgent.addAppStart(MainActivity.this);
		Toast.makeText(MainActivity.this, "onCreate" + "执行" + "addAppStart" + "启动完成", Toast.LENGTH_SHORT).show();

		initView();
	}

	private void initView() {
		mToggleButton=(ToggleButton) findViewById(R.id.tb_count);
		mToggleButton.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if (isChecked) {
					
				}else {
					
				}
			}
		});
		
		btn_debuggable=(Button)findViewById(R.id.btn_debuggable);
		btn_debuggable.setOnClickListener(mBtnClickListener);
		
		mPageVisit = (Button) findViewById(R.id.btn_pagevist);
		mPageVisit.setOnClickListener(mBtnClickListener);
		mAppStart = (Button) findViewById(R.id.btn_AppStart);
		mAppStart.setOnClickListener(mBtnClickListener);
		mUserAction = (Button) findViewById(R.id.btn_UserAction);
		mUserAction.setOnClickListener(mBtnClickListener);
		mUserActLocation = (Button) findViewById(R.id.btn_UserActLocation);
		mUserActLocation.setOnClickListener(mBtnClickListener);
		// mShare=(Button) findViewById(R.id.btn_share);
		// mShare.setOnClickListener(mBtnClickListener);

		mSpecialClick = (Button) findViewById(R.id.btn_SpecialClick);
		mSpecialClick.setOnClickListener(mBtnClickListener);
		mClientDown = (Button) findViewById(R.id.btn_ClientDown);
		mClientDown.setOnClickListener(mBtnClickListener);
		mOnEvent_simple = (Button) findViewById(R.id.btn_OnEvent_simple);
		mOnEvent_simple.setOnClickListener(mBtnClickListener);
		mOnEvent_Mult_Tag = (Button) findViewById(R.id.btn_OnEvent_Mult_Tag);
		mOnEvent_Mult_Tag.setOnClickListener(mBtnClickListener);
		mOnEvent_count = (Button) findViewById(R.id.btn_OnEvent_count);
		mOnEvent_count.setOnClickListener(mBtnClickListener);

		mOnEvent_KV = (Button) findViewById(R.id.btn_OnEvent_KV);
		mOnEvent_KV.setOnClickListener(mBtnClickListener);
		mUpAll = (Button) findViewById(R.id.btn_up_all);
		mUpAll.setOnClickListener(mBtnClickListener);
		
		mdb=(Button) findViewById(R.id.btn_db_query);
		mdb.setOnClickListener(mBtnClickListener);
		mdb_table=(Button) findViewById(R.id.btn_dbtable);
		mdb_table.setOnClickListener(mBtnClickListener);

		MobileClickAgent.setAppCode(this, appCode);
	}

	
	private OnClickListener mBtnClickListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			String productId = "productId";
			String productDesc = "productDesc";
			String sourceCode = "sourceCode";
			String sourceVersion = "sourceVersion";
			String categoryId = "categoryId";
			int chargeType = 22;
			int clickIndex = 11;

			switch (v.getId()) {
			case R.id.btn_pagevist:
				mIntent = new Intent();
				mIntent.setClass(MainActivity.this, PageVisitActivity.class);
				startActivity(mIntent);
				 //在startActivity()方法之后立即调用，设定新的Activity进入和
		        //当前Activity退出时的动画。
			//overridePendingTransition 方法必须在startActivity()或者 finish()方法的后面。
				overridePendingTransition(android.R.anim.fade_in,android.R.anim.slide_out_right);
				break;
			case R.id.btn_AppStart:
				// 实时上传wifi 3g 2g
				MobileClickAgent.addAppStart(MainActivity.this);
				// MobileClickAgent.addAppStart(context, appCode):
				// MobileClickAgent.addAppStart(context, ssoid, appCode);
				break;
			case R.id.btn_ClientDown:
				// MobileClickAgent.addDownload(MainActivity.this, productId,
				// productDesc, sourceCode, sourceVersion, categoryId,
				// chargeType, clickIndex);
				
				if (isCounted) {
					count++;
				}
				MobileClickAgent.addDownload(MainActivity.this, productId, productDesc, sourceCode, sourceVersion, categoryId, chargeType, clickIndex, appCode);
				break;
			case R.id.btn_OnEvent_count:
				// String eventID="eventID";
				// String eventTag="eventTag";
				// int eventCount=4;
				// MobileClickAgent.onEvent(MainActivity.this, eventID,
				// eventTag, eventCount,
				// MobileClickAgent.STATISTICS_STRATEGY_TIMELY_2G_3G_WIFI);

				mIntent = new Intent();
				mIntent.setClass(MainActivity.this, EventCountActivity.class);
				mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
				startActivity(mIntent);

				break;
			case R.id.btn_OnEvent_KV:
				// String eventID1="eventID1";
				// Map<String, String> eventMap=new HashMap<String, String>();
				// eventMap.put("key", "value");
				// MobileClickAgent.onKVEvent(MainActivity.this, eventID1,
				// eventMap,
				// MobileClickAgent.STATISTICS_STRATEGY_TIMELY_2G_3G_WIFI);

				mIntent = new Intent();
				mIntent.setClass(MainActivity.this, EventKVActivity.class);
				mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
				startActivity(mIntent);
				break;
			case R.id.btn_OnEvent_Mult_Tag:
				// String eventID11="eventID";
				// String eventTag1="eventTag";
				// MobileClickAgent.onEvent(MainActivity.this, eventID11,
				// eventTag1,
				// MobileClickAgent.STATISTICS_STRATEGY_TIMELY_2G_3G_WIFI);
				// MobileClickAgent.onEvent(MainActivity.this, eventID11,
				// eventTag1+1,
				// MobileClickAgent.STATISTICS_STRATEGY_TIMELY_2G_3G_WIFI);
				mIntent = new Intent();
				mIntent.setClass(MainActivity.this, EventMultTagActivity.class);
				mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
				startActivity(mIntent);

				break;
			case R.id.btn_OnEvent_simple:
				// String eventID111="eventID111";
				// MobileClickAgent.onEvent(MainActivity.this, eventID111,
				// MobileClickAgent.STATISTICS_STRATEGY_TIMELY_2G_3G_WIFI);
				mIntent = new Intent();
				mIntent.setClass(MainActivity.this, EventSimpleActivity.class);
				mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
				startActivity(mIntent);

				// MobileClickAgent.onEventStart(MainActivity.this, eventID,
				// MobileClickAgent.STATISTICS_STRATEGY_TIMELY_2G_3G_WIFI);
				// MobileClickAgent.onEventEnd(MainActivity.this, eventID,
				// MobileClickAgent.STATISTICS_STRATEGY_TIMELY_2G_3G_WIFI);

				// MobileClickAgent.onEventDuration(context, eventID, duration,
				// strategy);

				break;
			// case R.id.btn_share:
			// //一天 3G wifi
			// MobileClickAgent.addShared(MainActivity.this, pid, cid, shareto);
			// // MobileClickAgent.addShared(MainActivity.this, pid, cid,
			// shareto, appCode);
			// break;
			case R.id.btn_SpecialClick:
				MobileClickAgent.addSpecialClick(MainActivity.this, "3", "2", "1");
				// MobileClickAgent.addSpecialClick(MainActivity.this, "3", "2",
				// "1", appCode);
				break;
			case R.id.btn_UserAction:
				MobileClickAgent.addUserAction(MainActivity.this, actionCode);
				// MobileClickAgent.addUserAction(MainActivity.this, actionCode,
				// appCode);
				break;
			case R.id.btn_UserActLocation:
				MobileClickAgent.addUserActLocation(MainActivity.this, actionCode, actPosition, actOther);
				// MobileClickAgent.addUserActLocation(MainActivity.this,
				// actionCode, actPosition, actOther, appCode);
				break;
			case R.id.btn_up_all:
				MobileClickAgent.uploadAll(MainActivity.this);
				break;
			case R.id.btn_db_query:
				mIntent=new Intent();
				mIntent.setClass(MainActivity.this, DbTableActivity.class);
				startActivity(mIntent);
				break;
			case R.id.btn_dbtable:
				mIntent=new Intent();
				mIntent.setClass(MainActivity.this, DbTableListActivity.class);
				startActivity(mIntent);
				break;
			case R.id.btn_debuggable:
				mIntent=new Intent();
				mIntent.setClass(MainActivity.this, DebuggableFalse.class);
				startActivity(mIntent);
			default:
				break;
			}
		}
	};

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	
	 private long exitTime = 0;
	@Override
	public void onBackPressed() {
		if ((System.currentTimeMillis() - exitTime) > 2000)
        {
                Toast.makeText(MainActivity.this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
                exitTime = System.currentTimeMillis();
        } else
        {
                this.finish();
        }
//		super.onBackPressed();
	}
	
	
}
